package com.leon.spring_security_oauth2.web;

import com.leon.spring_security_oauth2.auth_util.R;
import com.leon.spring_security_oauth2.auth_util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestEndpoints {

    @Autowired
    @Qualifier("consumerTokenServices")
    private ConsumerTokenServices consumerTokenServices;

    @GetMapping("/product/{id}")
    public String getProduct(@PathVariable String id) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return "product id : " + id;
    }

    @GetMapping("/order/{id}")
    public String getOrder(@PathVariable String id) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return "order id : " + id;
    }

    @GetMapping("/user")
    public String getUserName() {
        return "Username : "+SecurityUtils.getUserDetailImpl().getUsername();
    }

    @GetMapping("/test")
    public String test() {
        return "test success! ";
    }
    /**
     * 清除Redis中 accesstoken refreshtoken
     *
     * @param access_token  accesstoken
     * @return true/false
     */
    @PostMapping("/oauth/logout")
    public R removeToken(String access_token) {
        consumerTokenServices.revokeToken(access_token);
        return R.ok("用户退出系统成功！");
    }


}
